package com.spider.task;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.db.mongodb.MMongoDB;
import com.httpclient.SpiderHttpClientManager;
import com.spider.base.BaseResult;
import com.spider.bean.ZhilianJobBean;
import com.spider.queue.SpiderBlockQueue;
import com.spider.util.SpiderCondition;

public class SpiderJobDetailTask implements Runnable {

	public void run() {
		parseJobDetailPage();
	}
	
	/** 解析工作详情页 获得页面中的 email
	 * @return
	 */
	public void parseJobDetailPage(){
		ZhilianJobBean res = getJobDetailContent();
		if(res==null)
			return ;
		Document doc = Jsoup.parse(res.getContent());
		Pattern pattern = Pattern.compile("^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)$");
		Elements e = doc.body().getElementsMatchingOwnText(pattern);
		if(e!=null){
			String cc = e.text();
			if(StringUtils.isNotBlank(cc)){
				System.out.println(res.getCompanyName()+"  "+res.getJobName() +"  "+cc);
				res.setContent("");
				res.setEmail(cc);
				//SpiderBlockQueue.emailQueue.offer(res);
				//存入数据库
				MMongoDB db = new MMongoDB();
				db.save(SpiderCondition.JOB_EMAIL_BEAN_TABLE_NAME, res);
			}
		}
	}
	
	
	/***
	 * 解析工作详情页url  页面总的内容
	 */
	public ZhilianJobBean getJobDetailContent(){
		ZhilianJobBean result = getBaseResultFromMongo();
		if(result==null)
			return null;
		String url = result.getJobUrl();
		if(StringUtils.isNotBlank(url)){
			HttpClient http = SpiderHttpClientManager.getInstance();
			GetMethod get = new GetMethod(url);
			try {
				get.setRequestHeader("Connection","keep-alive");
				get.setRequestHeader("Cookie","LastCity%5Fid=530; LastCity=%e5%8c%97%e4%ba%ac; dywez=95841923.1363184649.1.1.dywecsr=(direct)|dyweccn=(direct)|dywecmd=(none); __zpWAM=1363184690831.209630.1363184691.1363184691.1; dywea=95841923.1486548030822642000.1363184649.1363184649.1363184649.1; dywem=95841923.y; __utma=269921210.867832535.1363184648.1363184648.1363184648.1; __utmz=269921210.1363184648.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=269921210.|2=Member=395599436=1");
				get.setRequestHeader("Host","keep-alive");
				get.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1");  
				get.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
				get.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.5");   
				get.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); 
				get.setRequestHeader("Connection", "keep-alive"); 
				int code = http.executeMethod(get);
				if (code == HttpStatus.SC_OK) {
					byte[] r = get.getResponseBody();
					result.setContent(new String(r));
				}
			} catch (HttpException e) {
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				// 释放链接
				get.releaseConnection();
			}
		}
		return result;
	}
	
	
	
	/** 获得工作详情页的url
	 * @return
	 */
	public String getJobDetailurl(){
		ZhilianJobBean res = (ZhilianJobBean) getBaseResult();
		String result = null;
		if(res!=null){
			result = res.getJobUrl();
		}
		return result;
	}
	
	
	
	/** 从队列中获得 队列中存的对象
	 * @return
	 */
	public BaseResult getBaseResult(){
		BaseResult result = null;
		try {
			result = (BaseResult) SpiderBlockQueue.jobBeanQueue.poll(1, TimeUnit.SECONDS);
			if(result==null){
				result = (BaseResult) SpiderBlockQueue.jobBeanQueue2.poll(1, TimeUnit.SECONDS);
			}
			if(result==null)
				return null;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/** 从mongodb中获得 对象
	 * @return
	 */
	public ZhilianJobBean getBaseResultFromMongo(){
		ZhilianJobBean result = null;
		MMongoDB db = new MMongoDB();
		ZhilianJobBean fields = new ZhilianJobBean();
		fields.setCompanyName("1");
		fields.setJobName("1");
		fields.setJobUrl("1");
		result = db.findOne(SpiderCondition.JOB_BEAN_TABLE_NAME, new ZhilianJobBean(), fields);
		if(result==null)
			return null;
		db.remove(SpiderCondition.JOB_BEAN_TABLE_NAME, result);
		return result;
	}

}
